App({
	setting:{
		apiwebpath:'https://shtrj.029weixin.com',
		//apiwebpath:'http://www.shtrj.com',
		apiimgpath:'https://shtrj.029weixin.com/uploads/'
	},
	onLaunch:function(){

	},
	request:function(url, data, callback){
		wx.showLoading({
			title:'正在请求...'
		});
		var member_id = wx.getStorageSync('member_id');
		if(member_id){
			data.openid = member_id;
		}
		var that = this;
	    wx.request({
		    url:that.setting.apiwebpath + url,
		    data:data,
		    method:'POST',
			header:{'content-type':'application/json'},
			dataType:'json',
		    success:function(res){
				wx.hideLoading();
				if(res.statusCode != 200){
					wx.showToast({
						title:'请求失败',
						icon:'none',
					})
					return false;
				}
				else if(res.data.code == '40001'){
					//重新登陆
					wx.switchTab({
						url:'/pages/memberCenter/memberCenter'
					})
					return false;
				}
				callback(res.data);
			},
			fail:function(res){
				wx.hideLoading();
				wx.showToast({
				    title:'网络请求失败',
				    icon:'none',
				})
			}
		})
	},
	watchLogin:function(callback){
		var that = this;
		wx.login({
			success:function(res){
				var code = res.code;
				if(res.code){
					//获取基本信息
					wx.getUserInfo({
						success:function(res){
							wx.setStorageSync('wechat', res.userInfo);
							that.request('/api/Simple/wxLogin', {code:code}, function(res){
								if(res.code == '0'){
									//缓存数据
									wx.setStorageSync('member_id', res.data.openid);
									callback(res);
								}
							});
						},
						fail:function(res){
							wx.switchTab({
							    url:'/pages/memberCenter/memberCenter'
							})
						}						 
					})
				}
			}
		})
	}
})